package com.ming.note;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.ming.note.common.MyActivity;
import com.ming.note.util.ViewUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * author : changer
 * date   : 2020/6/11
 * desc   :主界面
 */
public class MainActivity extends MyActivity {

    @BindView(R.id.rvContent)
    RecyclerView rvContent;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        int spanCount = 2;
        StaggeredGridLayoutManager layoutManager =
                new StaggeredGridLayoutManager(spanCount, StaggeredGridLayoutManager.VERTICAL);
        rvContent.setLayoutManager(layoutManager);
        layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
        rvContent.setItemAnimator(null);
        rvContent.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                int[] first = new int[spanCount];
                layoutManager.findFirstCompletelyVisibleItemPositions(first);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && (first[0] == 1 || first[1] == 1)) {
                    layoutManager.invalidateSpanAssignments();
                }
            }
        });

        NoteAdapter adapter = new NoteAdapter();
        rvContent.setAdapter(adapter);
        ArrayList<String> datas = new ArrayList<>();
        for (int i = 0; i < 31; i++) {
            if (i % 2 == 0) {
                datas.add("dffffffffffffff");
            } else {
                datas.add("dffffffffffffffdddddddddddddddddddddddddddddddddddddddddddddddddddddddddd" +
                        "ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd" +
                        "ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd" +
                        "ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd" +
                        "ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd" +
                        "ddddddddddddddddddddddddddddddddddddddddddddddddddffffffffffffffffffffffffffff" +
                        "fffffffffff");
            }
        }
        adapter.setNewData(datas);
    }

    @Override
    protected void initData() {

    }

    class NoteAdapter extends BaseQuickAdapter<String, BaseViewHolder> {

        public NoteAdapter() {
            super(R.layout.item_note);
        }

        @Override
        protected void convert(BaseViewHolder baseViewHolder, String s) {
            int adapterPosition = baseViewHolder.getAdapterPosition();

            if (adapterPosition % 2 == 0) {
                baseViewHolder.itemView.setBackgroundColor(Color.parseColor("#33ff0000"));
            } else {
                baseViewHolder.itemView.setBackgroundColor(Color.parseColor("#3300ff00"));
            }

            TextView tvNoteItem = baseViewHolder.findView(R.id.tvNoteItem);
            ViewUtils.setText(tvNoteItem, s);
        }
    }
}
